package com.common.baselibrary.util;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class JsonUtil {
    private static Gson g = new Gson();

    public static <T> T fromJson(String json, Class<? extends T> clz) {
        T rtn = g.fromJson(json, clz);
        return rtn;
    }

    public static String toJson(Object o) {
        return g.toJson(o);
    }

    public static <T> List<T> toList(String json, Class<T[]> clazz) {
        Gson gson = new Gson();
        T[] array = gson.fromJson(json, clazz);
        List<T> resultList = new ArrayList<>();
        for (T t : array) {
            resultList.add(t);
        }
        return resultList;
    }
}
